package com.saclub.app.common;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.offroader.core.DoubleClickExitHelper;
import com.saclub.app.R;
import com.saclub.app.activity.MyToolBarActivity;
import com.saclub.app.fragment.BaseProgressFragment;

public abstract class LoadFragment extends BaseProgressFragment<MyToolBarActivity> implements IBack {

	private Fragment frag;
	private DoubleClickExitHelper mDoubleClickExit;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_tab;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {
		mDoubleClickExit = new DoubleClickExitHelper(getActivity());
	}

	@Override
	public void loadFragment(int containResId, Fragment frag, boolean showBackLast) {

		FragmentManager fm = getChildFragmentManager();

		FragmentTransaction ft = fm.beginTransaction();
		ft.addToBackStack(frag.getClass().getName());//保存当前的Frag到栈
		ft.replace(containResId, frag);//替换为新的Frag
		ft.commitAllowingStateLoss();

	}

	@Override
	public void onBackPressed() {

		FragmentManager fm = getChildFragmentManager();

		if (fm.getBackStackEntryCount() > 1) {

			fm.popBackStack();

		} else {

			mDoubleClickExit.onBackPressed();

		}
	}

}
